﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tomahawk.Runtime;
using Tomahawk.Runtime.Logic;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Components;
using Tomahawk.Runtime.Rendering.Materials;
using Tomahawk.Runtime.Rendering;

namespace Initialization.GameStates
{
    public class LightningActor : XActor
    {
        Random rand = new Random();
        Vector3 source;
        Vector3 destination;

        public Vector3 randomizeSource = Vector3.Zero;
        public Vector3 randomizeDestination = Vector3.Zero;

        public LightningActor()
        {
            UpdateRequired = true;
        }

        protected override void OnPostBeginPlay()
        {
            XLightningBoltComponent lightning = GetComponent<XLightningBoltComponent>();

            source = lightning.emitters[0].LightningBolt.Source;
            destination = lightning.emitters[0].LightningBolt.Destination;
        }

        protected override void OnUpdate(float elapsedTime)
        {
            XLightningBoltComponent lightning = GetComponent<XLightningBoltComponent>();

            lightning.emitters[0].LightningBolt.Destination = destination + (float)(rand.NextDouble() - 0.5) * randomizeDestination;
            lightning.emitters[0].LightningBolt.Source = source + (float)(rand.NextDouble() - 0.5) * randomizeSource;

            base.OnUpdate(elapsedTime);
        }
    }

    class BasicGameState : GameState
    {
        public LightningActor electricity1 = null;
        public LightningActor electricity2 = null;
        public LightningActor electricity3 = null;
        public LightningActor electricity4 = null;

        protected override void OnLoad()
        {
            electricity1 = (LightningActor)XWorld.Instance.CreateObject(typeof(LightningActor), "", @"Examples.LightningBolts.Content\lightningactor1.xml");
            electricity2 = (LightningActor)XWorld.Instance.CreateObject(typeof(LightningActor), "", @"Examples.LightningBolts.Content\lightningactor2.xml");
            electricity3 = (LightningActor)XWorld.Instance.CreateObject(typeof(LightningActor), "", @"Examples.LightningBolts.Content\lightningactor3.xml");
            electricity4 = (LightningActor)XWorld.Instance.CreateObject(typeof(LightningActor), "", @"Examples.LightningBolts.Content\lightningactor4.xml");
            
            electricity3.randomizeDestination = new Vector3(0, 20, 100);

            Engine.Instance.ResourceManager.LoadPendantResources(this.ContentManager);
        }

        protected override void OnUnload()
        {
        }

        protected override void OnInitialize()
        {
            Engine.Instance.CameraManager.SetActiveCamera(FreeCamComponent.label);
            Tomahawk.Runtime.Rendering.Camera camera = Engine.Instance.CameraManager.ActiveCamera;
            camera.LookAt(
                new Vector3(-250, 0, 0),
                new Vector3(0, 0, 0),
                Vector3.UnitY);
        }

        protected override void OnDestroy()
        {
        }
    }
}
